package com.sun.weekend.ui.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sun.weekend.R;
import com.sun.weekend.base.BaseFragment;
import com.sun.weekend.beans.UserBean;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

import static cn.bmob.v3.Bmob.getApplicationContext;

/**
 * @author sunleilei
 */

public class UserCenterRegisterFragment extends BaseFragment{
    EditText name;
    EditText password;
    Button register;
    Button query;
    TextView tv;
    ImageView imageView;

    @Override
    protected int getLayoutId() {
        return R.layout.user_register_activity;
    }

    @Override
    protected void findViews(View view) {
        name = (EditText) view.findViewById(R.id.name);
        password = (EditText) view.findViewById(R.id.password);
        register = (Button) view.findViewById(R.id.update);
        query = (Button) view.findViewById(R.id.query);
        tv = (TextView) view.findViewById(R.id.content);
        imageView = (ImageView) view.findViewById(R.id.imageview);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadData();
            }
        });
    }

    private void uploadData() {
        String n = name.getText().toString();
        String p = password.getText().toString();
        final UserBean user = new UserBean();
        user.setUsername(n);
        user.setPassword(p);
        final BmobFile file = new BmobFile(new File("/mnt/sdcard/a.jpg"));
        file.upload(new UploadFileListener() {
            @Override
            public void done(BmobException e) {

                user.setAvstar(file);
                user.signUp(new SaveListener<UserBean>() {
                    @Override
                    public void done(UserBean userBean, BmobException e) {
                        Glide.with(getApplicationContext()).load(userBean.getAvstar().getFileUrl()).into(imageView);
                    }
                });
            }
        });
    }
}
